<template>
  <div>
    <el-container>
      <el-aside :width="asideWidth" style="height:100vh;background-color:#001529;" >
        <div style="height:60px;color:white;display:flex;align-items:center;justify-content:center; ">
          <img src="../assets/avatar2.jpg" alt="" style="width:40px;height:40px;border-radius:20px;"/>
          <span style="margin-left:10px;font-size:20px;transition:all .3s;" v-show="!isCollapse">墨阁</span>
        </div>
        <el-menu :collapse="isCollapse" :collapse-transition="false" style="border:none;" background-color="#001529" text-color="rgba(255,255,255,0.65)" active-text-color="#fff" :default-active="$route.path" :router="true">
          <el-menu-item index="/home">
            <i class="el-icon-shopping-cart-full"></i>
            <span slot="title">订单管理</span>
          </el-menu-item>
          <el-menu-item index="/recharge">
            <i class="el-icon-money"></i>
            <span slot="title">会员充值</span>
          </el-menu-item>
          <el-submenu index="content">
            <template slot="title">
              <i class="el-icon-menu"></i>
              <span>内容管理</span>
            </template>
            <el-menu-item index="/activity">活动板块</el-menu-item>
            <el-menu-item index="/idea">理念板块</el-menu-item>
            <el-menu-item index="/recommend">推荐板块</el-menu-item>
            <el-menu-item index="/discount">折扣板块</el-menu-item>
          </el-submenu>
          <el-submenu index="info">
            <template slot="title">
              <i class="el-icon-s-platform"></i>
              <span>信息管理</span>
            </template>
            <el-menu-item index="/book">书籍信息</el-menu-item>
            <el-menu-item index="/user" v-if="user.role==='店长'">顾客信息</el-menu-item>
            <el-menu-item index="/publisher" v-if="user.role==='店长'">供应商信息</el-menu-item>
            <el-menu-item index="/vip" v-if="user.role==='店长'">会员信息</el-menu-item>
            <el-menu-item index="/clerk" v-if="user.role==='店长'">店员信息</el-menu-item>
          </el-submenu>
          <el-submenu index="shopping">
            <template slot="title">
              <i class="el-icon-s-goods"></i>
              <span>购售管理</span>
            </template>
            <el-menu-item index="/buy">购书管理</el-menu-item>
            <el-menu-item index="/sell">售书管理</el-menu-item>
          </el-submenu>
          <el-menu-item index="/profit" v-if="user.role==='店长'">
            <i class="el-icon-coin"></i>
            <span slot="title">利润分析</span>
          </el-menu-item>
        </el-menu>
      </el-aside>
      <el-container>
        <el-header>
          <i :class="collapseIcon" style="font-size:26px; " @click="handleCollapse"></i>
          <el-breadcrumb separator="/" style="margin-left:20px">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item :to="{ path: $route.path }">{{$route.meta.name}}</el-breadcrumb-item>
          </el-breadcrumb>
          <div style="flex:1;width:0;display:flex;align-items:center;justify-content:flex-end">
            <el-dropdown placement="bottom">
              <div style="display:flex;align-items:center;cursor:default;">
                <img :src="user.avatar || 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png'" alt="" style="width:40px;height:40px;border-radius:50%;margin-right:10px;"/>
                <span>{{user.name}}</span>
              </div>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item @click.native="$router.push('/person')">个人信息</el-dropdown-item>
                <el-dropdown-item @click.native="$router.push('/password')">修改密码</el-dropdown-item>
                <el-dropdown-item @click.native="logout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </div>
        </el-header>
        <!-- 主体区域 -->
        <el-main>
          <router-view @update:user="updateUser"/>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<style>
  *{
    padding: 0;
    margin: 0;
  }
  .el-menu--inline{
    background-color: #000c17 !important;
  }
  .el-menu--inline .el-menu-item{
    background-color: #000c17 !important;
    padding-left: 49px !important;
  }
  .el-menu-item:hover,.el-submenu__title:hover{
    color: #fff !important;
  }
  .el-submenu__title:hover i{
    color: #fff !important;
  }
  .el-menu-item.is-active{
    border-radius: 5px !important;
    background-color: #1890ff !important;
    width: calc(100% - 8px);
    margin-left: 4px;
  }

  .el-menu-item.is-active i,.el-menu-item.is-active .el-tooltip{
    margin-left: -4px;
  } 
  .el-menu-item,.el-submenu__title{
    height: 40px !important;
    line-height: 40px !important;
  }
  .el-submenu .el-menu-item{
    min-width: 0 !important;
  }
  .el-menu--inline .el-menu-item.is-active{
    padding-left: 45px !important;
  }
  .el-submenu .el-menu-item {
    min-width: 0 !important;
  }
  
  /*.el-submenu__icon-arrow{
    margin-top: -5px;
  }*/
  .el-aside{
    transition: width .3s;
    box-shadow: 2px 0 6px rgba(0,21,41,.35);
  }
  .el-header{
    box-shadow: 2px 0 6px rgba(0,21,41,.35);
    display: flex;
    align-items: center;
  }
  .el-main{
    height: calc(100vh - 60px);
    overflow-y: scroll;
  }
</style>
<script>

import request from "@/utils/request";

  export default{
    name: 'HomeView',
    data() {
        return {
            isCollapse:false,
            asideWidth:"200px",
            collapseIcon:'el-icon-s-fold',
            users: [],
            books: [],
            user: JSON.parse(localStorage.getItem('honey-user') || '{}')
        };
    },
    mounted() {
      if (!this.user.count) {   // 当前的浏览器没有顾客信息
        this.$router.push('/login')
      }
      request.get('/book/selectAll').then(res => {
          this.books = res.data;
          // console.log(res.data)
      });
      request.get('/user/selectAll').then(res => {
          this.users = res.data;
          // console.log(res.data)
      });
    },
    methods:{
      updateUser(user){//获取子组件传过来的数据，更新当前页面的数据
        this.user=JSON.parse(JSON.stringify(user))//让父级的对象和子级的对象毫无关联
      },
      logout(){
        localStorage.removeItem('honey-user')
        this.$router.push('/login')
      },
      handleCollapse(){
        this.isCollapse=!this.isCollapse
        this.asideWidth=this.isCollapse? '64px' : '200px'
        this.collapseIcon=this.isCollapse? 'el-icon-s-unfold' : 'el-icon-s-fold'
      }
    }
}
</script>